In this video we learn how to change our ROE index in a data frame.
And we have already seen that by default.
We have a range index starting from zero but in many cases it might be beneficial to ask a different
DRO index that enables us to label based indexing.
So but first of all let's import pandas and we import our Summer Olympic Games data set and start in
the variable summer.
And we have a look at the first five rows and we have a look at the index.
So we have a range index starting from zero and we have already learned that we can change the index
and select a column as the index and imparting our data with the read CSP method so we can say here
index column equals and let's say we want to have the athlete column as our index column so we can pass
athlete
and now we have few on the left side our athlete column s and our index off often the data frame and
we can check the index here
and here we can see our index is now at the athletes column.
So now what if we are not happy with our index and we want to change it back to the Range Index and
therefore we have the method reset index.
So let's just try it out here and we can see here on the left side we again half our Range Index and
our athlete column.
Tom Speck as a column so it's not any more index but a normal column of our data frame and we can also
check here the parameters and we have one parameter drop equals force.
So this is set by default to force.
So let's try it out again.
And what if he set this to true here then we completely drop our index that we had before.
So before we head to the athlete column as our index and if he said that the parameter dropped too true
we completely drop our index and replace it by the range and next and you can see here there is no athlete
column in our data frame so that's uh in this case actually not what we uh want.
So we change it back to false.
Now again we have our athlete column and our data frame and also in the Reset index method and there
is and the in-place parameter so by default it's set to fall.
So we do not change our original data frame and if we want to change our original data frame and change
the index back to the Range Index then we have to set of the in-place parameter to true.
So now if you run here so now we can see here that we do not get a view on a copy.
So this is a good indication that we changed our original data frame and let's check with the method
and then we can see here on the left side.
Again we have a Range Index and still we have our athlete column as a normal column of our data frame.
So now let's assume that before we we are not happy with the athlete column as in the index.
But now we want to have the year column as our index and there are actually two ways to do it.
So either we do this already when we import the data and set here the index card to year or if we have
already.
Well if we have already imported our data that is in the set index method to do this and let's press
shift tap here.
So the set next method to set to the data frame index using one or more existing columns.
So what we can do here actually we can pass them the column that we like to have as the index.
So in this case here in quotation marks and let us see what we get here then you're on the left side
as in the index we have in the year column.
All right so let's again press shift tap here and that's a parameter called drop.
And by default drop is set to true.
That means if we accept that the year column mass index it will be removed as an ordinary column.
And if we change this and accept the drop curve parameter to force the year column and not be removed
as a column.
So in the end we have the year column twice as an index and as an ordinary column.
So let's check this out.
So by default it's set to true
and we can see that we do not have a year column as column but if we accept this to false then we still
have the year column as column and s index.
So let's change it back here to true
and uh let's set up the in-place parameter to a true
and let's say again our data frame and now we have from the year column as index and we actually removed
the column from the column.
So there's no column year and now we can also check if the index is unique.
So I would assume that we get a false because already here we can see that we have a duplicate value
so eighteen hundred ninety six.
All right
so now we have learned how to change the complete index so we can reset our index to a range index and
we can set a complete column as our new index.
But what if you want to change only one element of our index and we can try this so let's have a look
here at our summer index.
So we fear our year columns index and the few select them.
The first element so here it's in the first element 896.
And if you try to set a new values of for example eighteen hundred ninety four when we get here an error
message index does not support mutable operations so with other words the index objects immutable and
we can also tried to set a new value for all elements of the index object so-called broadcasting so
we can say that for all elements in our index we want to set them the new value.
So the string here before 2016 and we can try this and again we get here an error message.
So this doesn't work but what we can do we can pass a complete list or sequence and that has exactly
the same amount of elements as our index.
Then as the new index.
So first of all let's check the size of our index.
So we have thirty one thousand one hundred sixty five elements in our index.
So if we pass a list or a sequence with is thirty one thousand one hundred sixty five new elements as
our new index then this should work.
And therefore we're creating here a list with thirty one thousand one hundred sixty five elements and
we want to have a new label.
And starting from medal number one better number two medal number three and so on.
So we want to Numa rate our medals from medal number one and two medal number thirty one thousand one
hundred sixty five.
And therefore we use here a list comprehension.
So for Ian Raines one to thirty one thousand one hundred sixty five including we name our elements and
our new list medal number one and number two and so on.
So now let's create our list here and let's have a look
so here you can see our list.
So it starts with medal number one and yeah in the end we're ending with a medal number thirty one thousand
one hundred sixty five.
And what we now can do we can say OK we want to set our index to a new index so new index here is our
list and let's try out if this works.
All right.
We do not get an error message and let's check in the first five rows and the last five rows and two
we can see that we have set our list new index as the index of our data frame and also check the last
five for us.
So starting from medal number one to the medal number thirty one thousand one hundred sixty five.
And we can also check if our row index only has a unique value.
So I would assume so yes.
All right.
So and that's a in addition a attribute called index stock name and let's check if we have a name already
for our index and here we run this.
And we see nothing so that's them at this point no name for our new index button but we can set a new
name by assigning for example a medal number.
So let's try this out and let's again check in the first five rows here and now here we can see that
we have a name for our index.
So we have medal number and now if we apply again the reset index method
we can see here on the left that we again have a range index and our index that we had before here medal
numbers.
Now a column of our data frame with the column label medal number.
All right so now we are finished service this video and I hope to see you on the next video by.